package coreModel

import (
	"database/sql/driver"
	"encoding/json"
	"errors"
	"fmt"
	"strings"
	"time"
)

// CustomDateTime 日期+时间
type CustomDateTime time.Time

var locTime *time.Location

func init() {
	// 加载东八区时区
	loc, err := time.LoadLocation("Asia/Shanghai")
	if err != nil {
		panic(err)
	}
	locTime = loc
}

func TimeNowTime() CustomDateTime {
	return CustomDateTime(time.Now())
}
func TimeNowDate() CustomDate {
	return CustomDate(time.Now())
}

func (c *CustomDateTime) Scan(value interface{}) error {
	switch v := value.(type) {
	case time.Time:
		*c = CustomDateTime(v)
	case []uint8:
		parseTime, err := time.ParseInLocation("2006-01-02 15:04:05", string(v), locTime)
		if err != nil {
			return err
		}
		*c = CustomDateTime(parseTime)
	case nil:
		*c = CustomDateTime(time.Time{})
	default:
		return errors.New("database time wrong")
	}
	return nil
}

func (c CustomDateTime) Local() time.Time {
	//判断时间是否为空
	if c == (CustomDateTime{}) {
		return time.Time{}
	}
	return time.Time(c).In(locTime).Local()
}

// IsEmpty 判断时间是否为空
func (c CustomDateTime) IsEmpty() bool {
	return time.Time(c).In(locTime).IsZero()
}

func (c CustomDateTime) MarshalJSON() ([]byte, error) {
	//判断时间是否为空
	if c == (CustomDateTime{}) {
		return []byte("null"), nil
	}

	formattedTime := time.Time(c).In(locTime).Format("2006-01-02 15:04:05")
	return []byte(fmt.Sprintf(`"%s"`, formattedTime)), nil
}

func (c *CustomDateTime) UnmarshalJSON(data []byte) error {
	var timeStr string
	if err := json.Unmarshal(data, &timeStr); err != nil {
		return err
	}

	if strings.TrimSpace(timeStr) != "" {
		t, err := time.ParseInLocation(time.DateTime, timeStr, locTime)
		if err != nil {
			return fmt.Errorf("failed to parse CustomDateTime from JSON: %v", err)
		}
		*c = CustomDateTime(t)
	} else {
		*c = CustomDateTime{}
	}

	return nil
}

// Value 实现 Valuer 接口，将自定义类型转换为数据库值
func (c CustomDateTime) Value() (driver.Value, error) {
	t := time.Time(c).In(locTime)
	if t.IsZero() {
		return nil, nil
	}
	return t, nil
}

// CustomDate 日期
type CustomDate time.Time

func (c *CustomDate) Scan(value interface{}) error {
	switch v := value.(type) {
	case time.Time:
		*c = CustomDate(v)
	case []uint8:
		parseTime, err := time.ParseInLocation("2006-01-02", string(v), locTime)
		if err != nil {
			return err
		}
		*c = CustomDate(parseTime)
	case nil:
		*c = CustomDate(time.Time{})
	default:
		return errors.New("database time wrong")
	}
	return nil
}

func (c CustomDate) Local() time.Time {
	return time.Time(c).In(locTime).Local()
}

func (c CustomDate) MarshalJSON() ([]byte, error) {
	//判断时间是否为空
	if c == (CustomDate{}) {
		return []byte("null"), nil
	}
	formattedTime := time.Time(c).In(locTime).Format("2006-01-02")
	return []byte(fmt.Sprintf(`"%s"`, formattedTime)), nil
}

func (c *CustomDate) UnmarshalJSON(data []byte) error {
	var timeStr string
	if err := json.Unmarshal(data, &timeStr); err != nil {
		return err
	}

	if strings.TrimSpace(timeStr) != "" {
		t, err := time.ParseInLocation(time.DateOnly, timeStr, locTime)
		if err != nil {
			return fmt.Errorf("failed to parse CustomDateTime from JSON: %v", err)
		}

		*c = CustomDate(t)
	} else {
		*c = CustomDate{}
	}

	return nil
}

// Value 实现 Valuer 接口，将自定义类型转换为数据库值
func (c CustomDate) Value() (driver.Value, error) {
	t := time.Time(c).In(locTime)
	if t.IsZero() {
		return nil, nil
	}
	return t, nil
}

// IsEmpty 判断时间是否为空
func (c CustomDate) IsEmpty() bool {
	return time.Time(c).In(locTime).IsZero()
}
